﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ASCII.Common;
using PlayDeezGames.Common;

namespace ASCII.ClickTheYellowBox
{
    public partial class MainPage : UserControl
    {
        private ASCIIRenderer renderer = new ASCIIRenderer();
        private CTYBStateMachine stateMachine;

        public MainPage()
        {
            InitializeComponent();
            renderer.GridBoard = Board;
            stateMachine = new CTYBStateMachine(renderer);
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            this.Focus();
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if(stateMachine.HandleBroadcast(new MessageWithPayload<KeyEventArgs>(CTYBStateMachine.MSGID_KeyDown, e),true))
            {
                stateMachine.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
        }

        private void Board_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (stateMachine.HandleBroadcast(new MessageWithPayload<MouseButtonEventArgs>(CTYBStateMachine.MSGID_MouseDown, e), true))
            {
                stateMachine.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
        }

        private void Board_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(stateMachine.HandleBroadcast(new MessageWithPayload<MouseButtonEventArgs>(CTYBStateMachine.MSGID_MouseUp, e), true))
            {
                stateMachine.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
        }

        private void Board_MouseMove(object sender, MouseEventArgs e)
        {
            if(stateMachine.HandleBroadcast(new MessageWithPayload<MouseEventArgs>(CTYBStateMachine.MSGID_MouseMove, e), true))
            {
                stateMachine.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
        }

        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            if(stateMachine.HandleBroadcast(new MessageWithPayload<KeyEventArgs>(CTYBStateMachine.MSGID_KeyUp, e), true))
            {
                stateMachine.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
        }
    }
}
